
#include <gl\glaux.h>
#include <stdio.h>	
#include "texturas.h"	

bool CargaTGA(TexturaTGA *textura, char *filename)	
{    

	GLubyte		TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};		// Uncompressed TGA Header
	GLubyte		TGAcompare[12];									// Used To Compare TGA Header
	GLubyte		header[6];										// First 6 Useful Bytes From The Header
	GLuint		bytesPerPixel;									// Holds Number Of Bytes Per Pixel Used In The TGA File
	GLuint		imageSize;										// Used To Store The Image Size When Setting Aside Ram
	GLuint		temp;											// Temporary Variable
	GLuint		type=GL_RGBA;									// Set The Default GL Mode To RBGA (32 BPP)
	
	FILE *file = fopen(filename, "rb");							// Open The TGA File

	if(	file==NULL ||											// Does File Even Exist?
		fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||	// Are There 12 Bytes To Read?
		memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0				||	// Does The Header Match What We Want?
		fread(header,1,sizeof(header),file)!=sizeof(header))				// If So Read Next 6 Header Bytes
	{
		if (file == NULL)										// Did The File Even Exist? *Added Jim Strong*
			return FALSE;										// Return False
		else													// Otherwise
		{
			fclose(file);										// If Anything Failed, Close The File
			return FALSE;										// Return False
		}
	}

	textura->ancho  = header[1] * 256 + header[0];				// Determine The TGA ancho	(highbyte*256+lowbyte)
	textura->alto = header[3] * 256 + header[2];				// Determine The TGA alto	(highbyte*256+lowbyte)
    
 	if(	textura->ancho	<=0	||									// Is The ancho Less Than Or Equal To Zero
		textura->alto	<=0	||									// Is The alto Less Than Or Equal To Zero
		(header[4]!=24 && header[4]!=32))						// Is The TGA 24 or 32 Bit?
	{
		fclose(file);											// If Anything Failed, Close The File
		return FALSE;											// Return False
	}

	textura->bpp	= header[4];								// Grab The TGA's Bits Per Pixel (24 or 32)
	bytesPerPixel	= textura->bpp/8;							// Divide By 8 To Get The Bytes Per Pixel
	imageSize		= textura->ancho*textura->alto*bytesPerPixel;	// Calculate The Memory Required For The TGA Data

	textura->imageData=(GLubyte *)malloc(imageSize);			// Reserve Memory To Hold The TGA Data

	if(	textura->imageData==NULL ||								// Does The Storage Memory Exist?
		fread(textura->imageData, 1, imageSize, file)!=imageSize)	// Does The Image Size Match The Memory Reserved?
	{
		if(textura->imageData!=NULL)							// Was Image Data Loaded
			free(textura->imageData);							// If So, Release The Image Data

		fclose(file);											// Close The File
		return FALSE;											// Return False
	}
	for(GLuint i=0; i<int(imageSize); i+=bytesPerPixel)			// Loop Through The Image Data
	{															// Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
		temp=textura->imageData[i];								// Temporarily Store The Value At Image Data 'i'
		textura->imageData[i] = textura->imageData[i + 2];		// Set The 1st Byte To The Value Of The 3rd Byte
		textura->imageData[i + 2] = temp;						// Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
	}

	fclose (file);												// Close The File

	// Build A textura From The Data
	
	glGenTextures(1, &textura->texID);						// Generate OpenGL textura IDs

	glBindTexture(GL_TEXTURE_2D, textura->texID);				// Bind Our Texture
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Filtered
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Filtered
	
	if (textura->bpp==24)										// Was The TGA 24 Bits
	{
		type=GL_RGB;											// If So Set The 'type' To GL_RGB
	}

	glTexImage2D(GL_TEXTURE_2D, 0, type, textura->ancho, textura->alto, 0, type, GL_UNSIGNED_BYTE, textura->imageData);

	return TRUE;												// Texture Building Went Ok, Return True
}

bool Esta(char *nomtext,AUX *auxText,int numtext,int t)
{
	t=0;
	bool esta=FALSE;

	while ((t<numtext)&&(!esta))
	{
		if (!strcmp(nomtext,auxText[t].nombre))
		{
			esta=TRUE;
		}
		t++;
	}
	return esta;
}